<?php
class GiftsController extends DefaultModuleController {
	
	public function actionList() {
	
		$pagestr = $sqladd = '';
		
		$page = max(1, getgpc('page'));
		$orderby = getgpc('orderby', 'G');
		$giftsId = getgpc(2);
		
		if(empty($giftsId)) {
			$sqladd = '';
		} elseif (!is_numeric($giftsId)) {
			$this->message('对不起，您访问的这个产品已经下线了！', 'gifts-list.htm');
		} else {
			$pagestr = "-$giftsId";
			$sqladd = "categoryId='$giftsId'";
		}
		
		$order = $orderby==1 ? 'credit DESC' : ($orderby==2 ? 'credit ASC' : '');
		$giftsList = $this->GiftsModel->findAll($sqladd, $page, GODHOUSE_PRODUCT_PPP, $order);
		
		foreach($giftsList as &$v) {
			$category = $this->CategoryModel->findByPk($v['categoryId']);
			$v['title'] = cutstr($v['title'],'22');
			$v['startDate'] = date('Y-m-d', $v['startDate']);
			$v['endDate'] = date('Y-m-d', $v['endDate']);
		}
		
		$num = $this->GiftsModel->count($sqladd);
		$multipage = $this->page($num, GODHOUSE_PRODUCT_PPP, $page, "gifts-list$pagestr.htm");

		$this->_showCategory();

		$this->view->assign('multipage', $multipage);
		$this->view->assign('giftsList', $giftsList);
		$this->view->assign('numCount', $num);
		$this->view->assign('giftsid', $giftsId);
		
		$this->display('gifts_list');
	}
	
	private function _showCategory()
	{
		$listArr = $this->CategorysPublic->get_list(GODHOUSE_CATEGORY_GIFTS, 2);		
		foreach ($listArr as $key => $val) {		
			if($val['level']==1) {
				$strArr = array();
				foreach ($listArr as $oval) {
					if($oval['parentId']==$val['categoryId']) {
						$strArr[] = array('categoryId' => $oval['categoryId'], '_cateName' => $oval['cateName'], 'cateName' => cutstr($oval['cateName'], 18, ''), 'giftsCount' => $oval['giftsCount']);
					}
				}
				$mlistArr[] = array('styleSwitch' => $i%2, 'categoryId' => $val['categoryId'], 'cateName' => $val['cateName'], 'moreArr' => $strArr);
			}
		}
		unset($listArr);
		$this->view->assign('cateArr', $mlistArr);
	}

	public function actionDetail() {
		$giftsId = getgpc(2);
		$gifts = $this->GiftsModel->findByPk($giftsId);
		// 格式化信息
		$gifts['startDate'] = date('Y-m-d', $gifts['startDate']);
		$gifts['endDate'] = date('Y-m-d', $gifts['endDate']);
		
		$this->_showCategory();
		
		$this->header['title'] .= $gifts['title'];
		$this->view->assign('gifts', $gifts);
		$this->display('gifts_detail');
	}
	
	/* 兑换物品 */
	public function actionConvert() {
		$giftsId = getgpc(2);
		if(!$this->userId) {
			$this->message('请您登录后再兑换物品。');
		}
		$gifts = $this->GiftsModel->findByPk($giftsId);
		if($gifts) {
			$giftsSwap = $this->GiftsSwapModel->find("userId='{$this->userId}' AND giftsId='$giftsId'");
			if($giftsSwap) {
				//$this->message('兑换失败，已经兑换了此物品。');
			}
			
			$time = $this->time;
			if($gifts['startDate'] > $time || $gifts['endDate'] < $time) {
				$this->message('兑换失败，不在兑换的日期范围内。');
			}
			if ($gifts['inventory']<=0) {
				$this->message('兑换失败，此物品暂时缺货。');
			}
			if($this->user['credit'] < $gifts['credit']) {
				$this->message('兑换失败，您的积分('.$this->user['credit'].')，不够兑换此物品。');
			}
			$giftsSwap = array(
				'title'=>$gifts['title'],
				'giftsId'=>$giftsId,
				'credit'=>$gifts['credit'],
				'userId'=>$this->userId,
				'userName'=>$this->userName,
				'createdTime'=>$time,
			);
			$this->GiftsSwapModel->insert($giftsSwap);
			$this->message('兑换成功，请等待管理员的审核。');
		} else {
			$this->message('兑换失败，兑换的物品不存在。');
		}
	}
	
	public function actionDelete() {
		if(!$this->userId) {
			$this->message('请您登记录后再操作。', GODHOUSE_DOMAIN_WWW.'user-gifts.htm');
		}
		
		$swapId = getgpc(2);
		if(!$swapId) {
			$this->message('操作失败，兑换礼品不存在。', GODHOUSE_DOMAIN_WWW.'user-gifts.htm');
		}
		
		$this->GiftsSwapModel->deleteByPk($swapId);
		$this->message('操作成功。', GODHOUSE_DOMAIN_WWW.'user-gifts.htm');
	}
}